﻿#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

//2.一个类默认创建：默认构造，析构，拷贝构造，operator=赋值运算符，进行简单的值传递；
class PersonAssign
{
public:
	PersonAssign(int a)
	{
		this->m_A = a;
	}

	int m_A;
};
void testPersonAssign()
{
	PersonAssign p1(10);
	PersonAssign p2(0);

	//1.赋值；
	p2 = p1;
	cout << "p2的m_A：" << p2.m_A << endl;//p2的m_A：10
}
class PersonHeap
{
public:
	PersonHeap(const char* name)
	{
		this->pName = new char[strlen(name) + 1];
		strcpy(this->pName, name);
	}
	//3.重载=，赋值运算符；
	PersonHeap& operator=(const PersonHeap& p)
	{
		//4.判断，如果原本已经有堆区内容，先释放；
		if (this->pName != NULL)
		{
			delete[] this->pName;
			this->pName = NULL;
		}
		this->pName = new char[strlen(p.pName) + 1];
		strcpy(this->pName, p.pName);
		return *this;
	}


	~PersonHeap()
	{
		delete[] this->pName;
		this->pName = NULL;
	}

	char* pName;
};
void testPersonHeap()
{
	PersonHeap p1("狗蛋");
	PersonHeap p2("狗剩");

	//5.必须要重载=运算符，否则会出现浅拷贝的问题；
	p2 = p1;

	PersonHeap p3("");
	p3 = p2 = p1;
	cout << p2.pName << endl;//狗蛋
	cout << p3.pName << endl;//狗蛋

	int a = 10;
	int b = 20;
	int c;
	c = a = b;
	cout << a << "  " << b << "  " << c << endl;//20  20  20
}

//main281：赋值运算符重载
int main281()
{
	testPersonAssign();
	testPersonHeap();

	system("pause");
	return EXIT_SUCCESS;
}